package com.lagou.handler;

import com.lagou.utils.ParameterMapping;

import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Objects;

public class ParameterHandler {

    public  void parse(PreparedStatement preparedStatement, Class<?> parameterType, List<ParameterMapping> parameterMappings, Object... params) throws Exception {
        if (Objects.nonNull(params) && params.length > 0) {
            int size = parameterMappings.size();
            for (int i = 1; i<= size; i++) {
                if (parameterType == Integer.class) {
                    preparedStatement.setObject(i,params[0]);
                } else {
                    String fieldName = parameterMappings.get(i-1).getContent();
                    Field field = parameterType.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    Object o = field.get(params[0]);
                    preparedStatement.setObject(i,o);
                }
            }
        }
    }
}
